package com.zhongke.cotrun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.*;
import com.zhongke.cotrun.model.pojo.*;
import com.zhongke.cotrun.service.SysMessageService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("sysMessageService")
public class SysMessageServiceImpl extends ServiceImpl<SysMessageDao, SysMessageEntity> implements SysMessageService {

    @Resource
    DictDao dictDao;    //数据字典dao
    @Resource
    InnovationActivitiesMapper innovationActivitiesMapper;  //创新活动dao
    @Resource
    public ProjectInfoMapper projectInfoMapper; //项目技术dao
    @Resource
    NeedInfoMapper needInfoMapper;  //需求dao

    /**
     * 传递参数
     *
     * @param dictType   数据字典type
     * @param linkItemId 关联项目id
     * @param type       //关联项目type
     * @return
     */
    @Override
    public SysMessageEntity saveSysMessage(String dictType, Long linkItemId, String type) {
        String topic = "";
        Long toUserId = null;
        switch (type) {
            case "activities":
                InnovationActivitiesEntityVo innovationActivitiesEntity = innovationActivitiesMapper.selectByID(linkItemId.intValue());
                if (innovationActivitiesEntity != null) {
                    topic = innovationActivitiesEntity.getActivitiesTopic();
                    toUserId = innovationActivitiesEntity.getCreateId();
                }
            case "demand":
                NeedInfo needInfo = needInfoMapper.selectDetailById(linkItemId);
                if (needInfo != null) {
                    topic = needInfo.getTitle();
                    toUserId = needInfo.getUserInfoId();
                }
            case "project":
                ProjectInfo projectInfo = projectInfoMapper.selectByPrimaryKey(linkItemId.intValue());
                if (projectInfo != null) {
                    topic = projectInfo.getProjectName();
                    toUserId = projectInfo.getCreateId();
                }
        }
        if (toUserId > 0) {
            DictEntity dictEntity = dictDao.getItemByType(dictType);
            SysMessageEntity messageEntity = new SysMessageEntity();
            messageEntity.setTitle(dictEntity.getValue());
            messageEntity.setContext(String.format(dictEntity.getRemark(), topic));
            messageEntity.setLinkItemType(type);
            messageEntity.setLinkItemId(linkItemId);
            messageEntity.setFromUserId(ShiroUtils.getUserId1());
            messageEntity.setToUserId(toUserId);
            save(messageEntity);
            return messageEntity;
        }
        return null;
    }

    @Override
    public PageInfo queryPage(SysMessageEntityVo entity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper wrapper = new QueryWrapper<SysMessageEntity>();
        if (entity.getReadFlag() != null)
            wrapper.eq("read_flag", entity.getReadFlag());
        if (entity.getToUserId() != null)
            wrapper.eq("to_user_id", entity.getToUserId());
        if (entity.getMessageType() != null)
            wrapper.eq("message_type", entity.getMessageType());
        wrapper.orderByDesc("create_time");
        List<SysMessageEntity> list = baseMapper.selectList(wrapper);
        return new PageInfo(list);
    }

}
